package com.example.logininterface;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.logininterface.Http.OkHttpUtil;
import com.example.logininterface.entity.User;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private String url = "http://192.168.1.251:20000/user/userInfo/loginByAccountAndPassword";
    private String url1 = "https://www.lindiaotong.com/lzzApis/user/userInfo/loginByAccountAndPassword";


    private String baseurl = "http://www.lindiaotong.com";
    private EditText accountEdittext;
    private EditText passwordEdittext;
    private Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        accountEdittext = (EditText) findViewById( R.id.account );
        passwordEdittext = (EditText) findViewById( R.id.password );
        loginButton = findViewById( R.id.login );

        loginButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdittext.getText().toString();
                String password = passwordEdittext.getText().toString();
                int patrol=0;


                if (TextUtils.isEmpty( account )) {
                    accountEdittext.setError( "账号不能为空" );
                    return;
                } else if (TextUtils.isEmpty( password )) {
                    passwordEdittext.setError( "密码不能为空" );
                    return;
                } else {
                    User user=new User(account,password,patrol);
                    Gson gson=new Gson();
                    String json=gson.toJson(user);
                    String args[]=new String[]{"lzzApis","user","userInfo","loginByAccountAndPassword"};

//                    String res= OkHttpUtil.postSyncRequest(baseurl,json,args);
//                    Log.d("同步:",res);
                    String res= OkHttpUtil.postAsyncRequest(baseurl,json,args);
                    Log.d("异步:",res);
//                    Map<String,String> map = new HashMap<>();
//                    Map<String, Integer> map1 = new HashMap<>();
//                    map.put( "account" , account );
//                    map.put( "password",password );
//                    map1.put( "patrol", patrol );
//                    JSONObject json = new JSONObject(map);
//                    JSONObject json1 = new JSONObject(map1);
//
//                    OkHttpClient okHttpClient = new OkHttpClient();
//                    FormBody formBody = new FormBody.Builder()
//                            .add( "map1", String.valueOf( json1 ) )
//                            .add( "map", String.valueOf( json ) )
//                            .build();
//                    Request request = new Request.Builder()
//                            .url( url )
//                            .post( formBody )
//                            .build();
//                    Call call = okHttpClient.newCall( request );
//                    call.enqueue( new Callback() {
//                        @Override
//                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                            //Toast.makeText( MainActivity.this,"服务器错误!",Toast.LENGTH_SHORT ).show();
//                            Log.e( "------", "连接失败" + e );
//                        }
//
//                        @Override
//                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                            String str = response.body().string();
//                            Log.d( "接收：", str );
//                        }
//                    } );
                }

            }
        } );
    }
    private void initListener(){

    }
}